<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>JS + CSS Clock</title>
</head>
<body>


    <div class="clock">
      <div class="clock-face">
        <div class="hand hour-hand"></div>
        <div class="hand min-hand"></div>
        <div class="hand second-hand"></div>
      </div>
    </div>


  <style>
    html {
      background: #018DED url(https://unsplash.it/1500/1000?image=881&blur=5);
      background-size: cover;
      font-family: 'helvetica neue';
      text-align: center;
      font-size: 10px;
    }

    body {
      margin: 0;
      font-size: 2rem;
      display: flex;
      flex: 1;
      min-height: 100vh;
      align-items: center;
    }

    .clock {
      width: 30rem;
      height: 30rem;
      border: 20px solid white;
      border-radius: 50%;
      margin: 50px auto;
      position: relative;
      padding: 2rem;
      box-shadow:
      0 0 0 2px rgba(0,0,0,0.1), 
      inset 0 0 0 3px #EFEFEF, 
      inset 0 0 10px black, 
      0 0 10px rgba(0,0,0,0.7)
    }

    .clock-face {
      position: relative;
      width: 100%;
      height: 100%;
      /* transform: translateY(-3px); account for the height of the clock hands */
    }
    .clock-face::after{
      content: '';
      background-color:#EFEFEF;
      width: 35px;
      height: 35px;
      border-radius: 50%;
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%,-50%);
    }

    .hand {
      position: absolute;
      height: 100%;
      width: 100%;
    }

    .second-hand::after{
      content: '';
      background-color:red;
      border-radius:100px;
      width: 3px;
      height: 50%;
      bottom: 50%;
      left: 50%;
      display: block;
      position: absolute;
      /* rotate:旋转 */
      transform: translate(-50%);
    }
    .min-hand::after{
      content: '';
      background-color:#EFEFEF;
      border-radius:100px;
      width: 6px;
      height: 40%;
      bottom: 50%;
      left: 50%;
      display: block;
      position: absolute;
      /* rotate:旋转 */
      transform: translate(-50%);
    }
    .hour-hand::after{
      content: '';
      background-color:#EFEFEF;
      border-radius:100px;
      width: 9px;
      height: 30%;
      bottom: 50%;
      left: 50%;
      display: block;
      position: absolute;
      /* rotate:旋转 */
      transform: translate(-50%);
    }

  </style>

  <script>
    // 安全性
    // IIFE立即调用函数表达式
    // 作用域控制
    // 只执行一次
    // 隔离作用域，防止污染全局命名空间
    // ; 避免code和code连在一起
    ;(function(){
      const second =document.querySelector('.second-hand');
      const min = document.querySelector('.min-hand');
      const hour = document.querySelector('.hour-hand');
      
      function setClock(){
        const date = new Date();
        const secondDeg = date.getSeconds()*6;
        const minDeg = date.getMinutes()*6 + date.getSeconds()*6/60;
        const hourDeg = date.getHours()*30 +date.getMinutes()*30/60; // 360/12
        second.style.transform = `rotate(${secondDeg}deg)`;
        min.style.transform = `rotate(${minDeg}deg)`;
        hour.style.transform = `rotate(${hourDeg}deg)`;
      }
      function timeoutHandler(){
        setClock();
        setTimeout(timeoutHandler,1000);
      }
      function animationHandler(){
        setClock();
        window.requestAnimationFrame(animationHandler);
      }
      setClock();
      // 三种计时器 setInterval setTimeout requesanimationFrame
      // setInterval(setClock,1000);//设定时间 持续执行
      setTimeout(timeoutHandler,1000);//设置间隔 执行一次    延迟
      // window.requestAnimationFrame(animationHandler);//处理画面更新的 timeout
      // 画面更新
    })()

  </script>
</body>
</html>
